home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / code.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  10KB  |  344 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """Utilities needed to emulate Python's interactive interpreter.
  5.  
  6. """
  7. import sys
  8. import traceback
  9. from codeop import CommandCompiler, compile_command
  10. __all__ = [
  11.     'InteractiveInterpreter',
  12.     'InteractiveConsole',
  13.     'interact',
  14.     'compile_command']
  15.  
  16. def softspace(file, newvalue):
  17.     oldvalue = 0
  18.     
  19.     try:
  20.         oldvalue = file.softspace
  21.     except AttributeError:
  22.         pass
  23.  
  24.     
  25.     try:
  26.         file.softspace = newvalue
  27.     except (AttributeError, TypeError):
  28.         pass
  29.  
  30.     return oldvalue
  31.  
  32.  
  33. class InteractiveInterpreter:
  34.     """Base class for InteractiveConsole.
  35.  
  36.     This class deals with parsing and interpreter state (the user's
  37.     namespace); it doesn't deal with input buffering or prompting or
  38.     input file naming (the filename is always passed in explicitly).
  39.  
  40.     """
  41.     
  42.     def __init__(self, locals = None):
  43.         '''Constructor.
  44.  
  45.         The optional \'locals\' argument specifies the dictionary in
  46.         which code will be executed; it defaults to a newly created
  47.         dictionary with key "__name__" set to "__console__" and key
  48.         "__doc__" set to None.
  49.  
  50.         '''
  51.         if locals is None:
  52.             locals = {
  53.                 '__name__': '__console__',
  54.                 '__doc__': None }
  55.         
  56.         self.locals = locals
  57.         self.compile = CommandCompiler()
  58.  
  59.     
  60.     def runsource(self, source, filename = '<input>', symbol = 'single'):
  61.         '''Compile and run some source in the interpreter.
  62.  
  63.         Arguments are as for compile_command().
  64.  
  65.         One several things can happen:
  66.  
  67.         1) The input is incorrect; compile_command() raised an
  68.         exception (SyntaxError or OverflowError).  A syntax traceback
  69.         will be printed by calling the showsyntaxerror() method.
  70.  
  71.         2) The input is incomplete, and more input is required;
  72.         compile_command() returned None.  Nothing happens.
  73.  
  74.         3) The input is complete; compile_command() returned a code
  75.         object.  The code is executed by calling self.runcode() (which
  76.         also handles run-time exceptions, except for SystemExit).
  77.  
  78.         The return value is True in case 2, False in the other cases (unless
  79.         an exception is raised).  The return value can be used to
  80.         decide whether to use sys.ps1 or sys.ps2 to prompt the next
  81.         line.
  82.  
  83.         '''
  84.         
  85.         try:
  86.             code = self.compile(source, filename, symbol)
  87.         except (OverflowError, SyntaxError, ValueError):
  88.             self.showsyntaxerror(filename)
  89.             return False
  90.  
  91.         if code is None:
  92.             return True
  93.         self.runcode(code)
  94.         return False
  95.  
  96.     
  97.     def runcode(self, code):
  98.         '''Execute a code object.
  99.  
  100.         When an exception occurs, self.showtraceback() is called to
  101.         display a traceback.  All exceptions are caught except
  102.         SystemExit, which is reraised.
  103.  
  104.         A note about KeyboardInterrupt: this exception may occur
  105.         elsewhere in this code, and may not always be caught.  The
  106.         caller should be prepared to deal with it.
  107.  
  108.         '''
  109.         
  110.         try:
  111.             exec code in self.locals
  112.         except SystemExit:
  113.             raise 
  114.         except:
  115.             self.showtraceback()
  116.  
  117.         if softspace(sys.stdout, 0):
  118.             print 
  119.         
  120.  
  121.     
  122.     def showsyntaxerror(self, filename = None):
  123.         '''Display the syntax error that just occurred.
  124.  
  125.         This doesn\'t display a stack trace because there isn\'t one.
  126.  
  127.         If a filename is given, it is stuffed in the exception instead
  128.         of what was there before (because Python\'s parser always uses
  129.         "<string>" when reading from a string).
  130.  
  131.         The output is written by self.write(), below.
  132.  
  133.         '''
  134.         (type, value, sys.last_traceback) = sys.exc_info()
  135.         sys.last_type = type
  136.         sys.last_value = value
  137.         if filename and type is SyntaxError:
  138.             
  139.             try:
  140.                 (dummy_filename, lineno, offset, line) = (msg,)
  141.             except:
  142.                 pass
  143.  
  144.             value = SyntaxError(msg, (filename, lineno, offset, line))
  145.             sys.last_value = value
  146.         
  147.         list = traceback.format_exception_only(type, value)
  148.         map(self.write, list)
  149.  
  150.     
  151.     def showtraceback(self):
  152.         '''Display the exception that just occurred.
  153.  
  154.         We remove the first stack item because it is our own code.
  155.  
  156.         The output is written by self.write(), below.
  157.  
  158.         '''
  159.         
  160.         try:
  161.             (type, value, tb) = sys.exc_info()
  162.             sys.last_type = type
  163.             sys.last_value = value
  164.             sys.last_traceback = tb
  165.             tblist = traceback.extract_tb(tb)
  166.             del tblist[:1]
  167.             list = traceback.format_list(tblist)
  168.             if list:
  169.                 list.insert(0, 'Traceback (most recent call last):\n')
  170.             
  171.             list[len(list):] = traceback.format_exception_only(type, value)
  172.         finally:
  173.             tblist = None
  174.             tb = None
  175.  
  176.         map(self.write, list)
  177.  
  178.     
  179.     def write(self, data):
  180.         '''Write a string.
  181.  
  182.         The base implementation writes to sys.stderr; a subclass may
  183.         replace this with a different implementation.
  184.  
  185.         '''
  186.         sys.stderr.write(data)
  187.  
  188.  
  189.  
  190. class InteractiveConsole(InteractiveInterpreter):
  191.     '''Closely emulate the behavior of the interactive Python interpreter.
  192.  
  193.     This class builds on InteractiveInterpreter and adds prompting
  194.     using the familiar sys.ps1 and sys.ps2, and input buffering.
  195.  
  196.     '''
  197.     
  198.     def __init__(self, locals = None, filename = '<console>'):
  199.         '''Constructor.
  200.  
  201.         The optional locals argument will be passed to the
  202.         InteractiveInterpreter base class.
  203.  
  204.         The optional filename argument should specify the (file)name
  205.         of the input stream; it will show up in tracebacks.
  206.  
  207.         '''
  208.         InteractiveInterpreter.__init__(self, locals)
  209.         self.filename = filename
  210.         self.resetbuffer()
  211.  
  212.     
  213.     def resetbuffer(self):
  214.         '''Reset the input buffer.'''
  215.         self.buffer = []
  216.  
  217.     
  218.     def interact(self, banner = None):
  219.         """Closely emulate the interactive Python console.
  220.  
  221.         The optional banner argument specify the banner to print
  222.         before the first interaction; by default it prints a banner
  223.         similar to the one printed by the real Python interpreter,
  224.         followed by the current class name in parentheses (so as not
  225.         to confuse this with the real interpreter -- since it's so
  226.         close!).
  227.  
  228.         """
  229.         
  230.         try:
  231.             sys.ps1
  232.         except AttributeError:
  233.             sys.ps1 = '>>> '
  234.  
  235.         
  236.         try:
  237.             sys.ps2
  238.         except AttributeError:
  239.             sys.ps2 = '... '
  240.  
  241.         cprt = 'Type "help", "copyright", "credits" or "license" for more information.'
  242.         if banner is None:
  243.             self.write('Python %s on %s\n%s\n(%s)\n' % (sys.version, sys.platform, cprt, self.__class__.__name__))
  244.         else:
  245.             self.write('%s\n' % str(banner))
  246.         more = 0
  247.         while None:
  248.             
  249.             try:
  250.                 if more:
  251.                     prompt = sys.ps2
  252.                 else:
  253.                     prompt = sys.ps1
  254.                 
  255.                 try:
  256.                     line = self.raw_input(prompt)
  257.                     encoding = getattr(sys.stdin, 'encoding', None)
  258.                     if encoding and not isinstance(line, unicode):
  259.                         line = line.decode(encoding)
  260.                 except EOFError:
  261.                     self.write('\n')
  262.                     break
  263.  
  264.                 more = self.push(line)
  265.             continue
  266.             except KeyboardInterrupt:
  267.                 self.write('\nKeyboardInterrupt\n')
  268.                 self.resetbuffer()
  269.                 more = 0
  270.                 continue
  271.             
  272.  
  273.             return None
  274.  
  275.     
  276.     def push(self, line):
  277.         """Push a line to the interpreter.
  278.  
  279.         The line should not have a trailing newline; it may have
  280.         internal newlines.  The line is appended to a buffer and the
  281.         interpreter's runsource() method is called with the
  282.         concatenated contents of the buffer as source.  If this
  283.         indicates that the command was executed or invalid, the buffer
  284.         is reset; otherwise, the command is incomplete, and the buffer
  285.         is left as it was after the line was appended.  The return
  286.         value is 1 if more input is required, 0 if the line was dealt
  287.         with in some way (this is the same as runsource()).
  288.  
  289.         """
  290.         self.buffer.append(line)
  291.         source = '\n'.join(self.buffer)
  292.         more = self.runsource(source, self.filename)
  293.         if not more:
  294.             self.resetbuffer()
  295.         
  296.         return more
  297.  
  298.     
  299.     def raw_input(self, prompt = ''):
  300.         '''Write a prompt and read a line.
  301.  
  302.         The returned line does not include the trailing newline.
  303.         When the user enters the EOF key sequence, EOFError is raised.
  304.  
  305.         The base implementation uses the built-in function
  306.         raw_input(); a subclass may replace this with a different
  307.         implementation.
  308.  
  309.         '''
  310.         return raw_input(prompt)
  311.  
  312.  
  313.  
  314. def interact(banner = None, readfunc = None, local = None):
  315.     '''Closely emulate the interactive Python interpreter.
  316.  
  317.     This is a backwards compatible interface to the InteractiveConsole
  318.     class.  When readfunc is not specified, it attempts to import the
  319.     readline module to enable GNU readline if it is available.
  320.  
  321.     Arguments (all optional, all default to None):
  322.  
  323.     banner -- passed to InteractiveConsole.interact()
  324.     readfunc -- if not None, replaces InteractiveConsole.raw_input()
  325.     local -- passed to InteractiveInterpreter.__init__()
  326.  
  327.     '''
  328.     console = InteractiveConsole(local)
  329.     if readfunc is not None:
  330.         console.raw_input = readfunc
  331.     else:
  332.         
  333.         try:
  334.             import readline as readline
  335.         except ImportError:
  336.             pass
  337.  
  338.     console.interact(banner)
  339.  
  340. if __name__ == '__main__':
  341.     import pdb
  342.     pdb.run('interact()\n')
  343.  
  344.